package com.liyuxiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liyuxiang.constants.SystemConstants;
import com.liyuxiang.mapper.CategoryMapper;
import com.liyuxiang.pojo.entity.Article;
import com.liyuxiang.pojo.entity.Category;
import com.liyuxiang.pojo.vo.ArticleDetailVo;
import com.liyuxiang.pojo.vo.ArticleVo;
import com.liyuxiang.pojo.vo.HotArticleVo;
import com.liyuxiang.pojo.vo.PageVo;
import com.liyuxiang.service.ArticleService;
import com.liyuxiang.mapper.ArticleMapper;
import com.liyuxiang.utils.BeanCopyUtils;
import com.liyuxiang.vm.ResponseResult;
import com.liyuxiang.vm.enums.ResultStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author liyuxiang
 * @description 针对表【sg_article(文章表)】的数据库操作Service实现
 * @createDate 2022-03-26 14:24:41
 */
@Service
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public ResponseResult getHotArticleList() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //必须是正式文章
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        //按照浏览量进行排序
        queryWrapper.orderByDesc(Article::getViewCount);
        //最多10条记录
        Page<Article> articlePage = new Page<>(1, 10);
        page(articlePage, queryWrapper);

//        List<HotArticleVo> articleVoList = new ArrayList<>();
        //Bean拷贝
//        for (Article article : articlePage.getRecords()) {
//            HotArticleVo hotArticleVo = new HotArticleVo();
//            BeanUtils.copyProperties(article,hotArticleVo);
//            articleVos.add(hotArticleVo);
//        }
//        List<HotArticleVo> articleVoList = BeanCopyUtils.copyBeanList(articlePage.getRecords(), HotArticleVo.class);
        return ResponseResult.okResult(BeanCopyUtils.copyBeanList(articlePage.getRecords(), HotArticleVo.class));
    }

    @Override
    public ResponseResult articleList(Integer pageNum, Integer pageSize, Long categoryId) {

        //先查询文章分类的id，条件是状态为0
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>().eq(Category::getStatus, SystemConstants.CATEGORY_STATUS_NORMAL));
        categoryList.forEach(cate -> System.out.println(cate.toString()));
        Set<Long> categoryIdSet = categoryList.stream().map(category -> category.getId()).collect(Collectors.toSet());
        log.info("获取到的正式分类id：{}", categoryIdSet);
        //查询所有的文章，条件是文章的分类id包含categoryIdSet
        //判断前端是否传入categoryId
        //分类的id必须正式分类
        /**
         * 1.eq()   文章的状态必须是0
         * 2.eq()   判断前端是否传入categoryId
         * 3.in()   查询文章的分类id必须在上方 categoryIdSet 结果中
         */
        LambdaQueryWrapper<Article> articleLambdaQueryWrapper = new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL)
                .eq(categoryId != null && categoryId > 0, Article::getCategoryId, categoryId)
                .in(Article::getCategoryId, categoryIdSet);
        Page<Article> articlePage = new Page<>(pageNum, pageSize);
        page(articlePage, articleLambdaQueryWrapper);
        List<ArticleVo> articleVos = BeanCopyUtils.copyBeanList(articlePage.getRecords(), ArticleVo.class);
        //遍历查询到的分类，把文章vo的分类名称设置属性值
//        for (ArticleVo articleVo : articleVos) {
//            //为文章vo类设置分类名称，这里需要判断分类的id必须等于文章的分类id才可以赋值
//            articleVo.setCategoryName(categoryList.stream().filter(category -> category.getId() == articleVo.getCategoryId()).findFirst().get().getName());
//        }
        //函数式编程
        articleVos.forEach(articleVo -> articleVo.setCategoryName(categoryList.stream().filter(category -> category.getId() == articleVo.getCategoryId()).findFirst().get().getName()));
        return new ResponseResult().ok(new PageVo(articleVos, (int) articlePage.getTotal()));
    }

    @Override
    public ResponseResult getArticleById(Long id) {
        Article article = articleMapper.getArticleById(id);
        if (article == null) {
            return new ResponseResult(10001,"该文章不存在");
        }
        System.out.println(article.toString());
        ArticleDetailVo articleDetailVo = BeanCopyUtils.copyBean(article, ArticleDetailVo.class);
        System.out.println(articleDetailVo);
        return new ResponseResult().ok(articleDetailVo);
    }
}




